package cn.orangeframe.neon.ui.web;

import cn.orangeframe.neon.application.command.MetadataCommand;
import cn.orangeframe.neon.domain.entity.WorkflowDef;
import cn.orangeframe.neon.infrastructure.convertor.GraphToWorkflow;
import cn.orangeframe.neon.ui.model.Response;
import cn.orangeframe.neon.domain.entity.GraphData;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("metadata")
public class MetadataController {

    private final MetadataCommand metadataCommand;

    public MetadataController(MetadataCommand metadataCommand) {
        this.metadataCommand = metadataCommand;
    }

    /**
     * 获取流程列表
     * @return Response 响应对象
     */
    @PostMapping("workflowList")
    public Response workflowList() {
        List<WorkflowDef> result = metadataCommand.getWorkflowDefList();
        return Response.success(result);
    }

    /**
     * 获取指定工作流名称和版本的工作流定义
     * @param workflowName 工作流名称
     * @param version 版本
     * @return Response 响应对象
     */
    @GetMapping("workflow/{workflowName}/{version}")
    public Response workflow(@PathVariable String workflowName, @PathVariable Integer version) {
        WorkflowDef result = metadataCommand.getWorkflowDef(workflowName, version);
        return Response.success(result);
    }

    /**
     * 获取指定工作流名称和版本的工作流定义的流程图
     * @param workflowName 工作流名称
     * @param version 版本
     * @return Response 响应对象
     */
    @GetMapping("graphWorkflow/{workflowName}/{version}")
    public Response graphWorkflow(@PathVariable String workflowName, @PathVariable Integer version) {
        GraphData result = metadataCommand.getGraphWorkflowDef(workflowName, version);
        return Response.success(result);
    }

    /**
     * 更新工作流定义
     * @param workflowDef 工作流定义
     * @return Response 响应对象
     */
    @PostMapping("workflow/create")
    public Response updateWorkflow(@RequestBody WorkflowDef workflowDef) {
        metadataCommand.registerOrUpdate(workflowDef);
        return Response.success();
    }

    /**
     * 创建工作流
     * @param workflowDef 工作流定义
     * @return Response 响应对象
     */
    @PutMapping("workflow/{workflowName}/{version}")
    public Response registerWorkflow(@RequestBody WorkflowDef workflowDef) {
        metadataCommand.registerOrUpdate(workflowDef);
        return Response.success();
    }

    /**
     * 根据流程图创建或更新流程
     * @param graphData
     * @return
     */
    @PostMapping("registerOrUpdateByGraph")
    public Response registerOrUpdateByGraph(@RequestBody GraphData graphData) {
        metadataCommand.registerOrUpdateGraph(graphData);
        return Response.success();
    }

    /**
     * 启动工作流(无入参)
     * @param workflowName 工作流名称
     * @param version 版本
     * @return Response 响应对象
     */
    @PostMapping("start/{version}/{workflowName}")
    public Response startWorkflow(@PathVariable("version") Integer version, @PathVariable("workflowName") String workflowName) {
        return Response.success(metadataCommand.startWorkflow(workflowName, version, null));
    }

    /**
     * 启动工作流
     * @param workflowName 工作流名称
     * @param version 版本
     * @param input 输入参数
     * @return Response 响应对象
     */
    @PostMapping("startWithInput/{version}/{workflowName}")
    public Response startWorkflow(@PathVariable("version") Integer version, @PathVariable("workflowName") String workflowName
            , @RequestBody Map<String, Object> input) {
        return Response.success(metadataCommand.startWorkflow(workflowName, version, input));
    }
}
